package com.github.florent37.shapeofview.shapes;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import com.github.florent37.shapeofview.manager.ResourceTable;

/**
 * 自定义三角形状
 *
 * @since 2021-07-22
 */
public class StarView extends StackLayout implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint paint = new Paint();
    private int noOfPoints = 5;
    private int width;
    private int height;
    private PixelMapHolder pmh01;

    /**
     * StarView
     *
     * @param context context
     */
    public StarView(Context context) {
        super(context);
        init(context, null);
    }
    /**
     * StarView
     *
     * @param context context
     * @param attrs attrs
     */
    public StarView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
    /**
     * StarView
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public StarView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, String.valueOf(defStyleAttr));
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL_STYLE);
        paint.setStrokeWidth(10);
        paint.setStrokeJoin(Paint.Join.MITER_JOIN);
        if (attrs != null) {
            final int points = attrs.getAttr("shape_star_noOfPoints").get().getIntegerValue();
            noOfPoints = points > 2 ? points : noOfPoints;
        }
        setEstimateSizeListener(this::onEstimateSize);
        addDrawTask(this::onDraw);
    }

    @Override
    public boolean onEstimateSize(int mwidth, int mheight) {
        width = EstimateSpec.getSize(mwidth);
        height = EstimateSpec.getSize(mheight);
        System.out.println("onEstimateSize  : height" + height);
        System.out.println("onEstimateSize width : " + width);
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int savacont = canvas.saveLayer(new RectFloat(0, 0, 1080, 2000), paint);
        final int vertices = noOfPoints * 2;
        final float alpha = (float) (2 * Math.PI) / vertices;
        final float radius = (float) (height <= width ? height : width) / 2;
        final float centerX = width / 2;
        final float centerY = height / 2;
        final Path path = new Path();
        for (int i = vertices + 1; i != 0; i--) {
            float r = radius * (i % 2 + 1) / 2;
            double omega = alpha * i;
            path.lineTo((float) (r * Math.sin(omega)) + centerX, (float) (r * Math.cos(omega)) + centerY);
        }
        path.close();
        canvas.drawPath(path, paint);
        paint.setBlendMode(BlendMode.SRC_IN);
        pmh01 = new PixelMapHolder(getPixelMap(ResourceTable.Media_captain_bk));
        canvas.drawPixelMapHolder(pmh01, 0, 0, paint);
        paint.setBlendMode(null);
        canvas.restoreToCount(savacont);
    }

    private PixelMap getPixelMap(int resourceId) {
        try {
            Resource resource = getResourceManager().getResource(resourceId);
            ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(imageSource.getImageInfo().size.width + 90, imageSource.getImageInfo().size.height + 160);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);

            width = pixelMap.getImageInfo().size.width;
            height = pixelMap.getImageInfo().size.height;
            System.out.println("getheight : " + height);
            System.out.println("width : " + width);

            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
